package org.v2ray.rocket.proxy.support.base

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.v2ray.rocket.proxy.support.base.mvi.MviEffect
import org.v2ray.rocket.proxy.support.base.mvi.MviIntent
import org.v2ray.rocket.proxy.support.base.mvi.MviState
import www.sanju.motiontoast.MotionToastStyle

/**
 * Toast事件数据类
 */
data class ToastEvent(val message: String, val style: MotionToastStyle)

/**
 * MVI架构的基础ViewModel
 * @param I Intent类型
 * @param S State类型
 * @param E Effect类型
 */
abstract class BaseViewModel<I : MviIntent, S : MviState, E : MviEffect> : ViewModel() {

    // 初始状态
    private val initialState: S by lazy { createInitialState() }

    // 当前状态
    private val _viewState: MutableStateFlow<S> = MutableStateFlow(initialState)
    val viewState: StateFlow<S> = _viewState.asStateFlow()

    // 一次性事件
    private val _effect: MutableSharedFlow<E> = MutableSharedFlow()
    val effect: SharedFlow<E> = _effect.asSharedFlow()

    // Toast事件
    private val _toastEvent: MutableSharedFlow<ToastEvent> = MutableSharedFlow()
    val toastEvent: SharedFlow<ToastEvent> = _toastEvent.asSharedFlow()

    // Intent处理
    private val _intent: MutableSharedFlow<I> = MutableSharedFlow()

    init {
        subscribeIntents()
    }

    /**
     * 创建初始状态
     */
    abstract fun createInitialState(): S

    /**
     * 处理Intent
     */
    abstract fun handleIntent(intent: I)

    /**
     * 设置新的状态
     */
    protected fun setState(reduce: S.() -> S) {
        val newState = viewState.value.reduce()
        _viewState.value = newState
    }

    /**
     * 发送一次性事件
     */
    protected fun setEffect(builder: () -> E) {
        val effectValue = builder()
        viewModelScope.launch { _effect.emit(effectValue) }
    }

    /**
     * 显示Toast消息
     */
    protected fun showToast(message: String,style: MotionToastStyle) {
        // 直接通过SharedFlow发送Toast事件，不通过Effect
        viewModelScope.launch {
            _toastEvent.emit(ToastEvent(message, style))
        }
    }

    /**
     * 显示Loading状态
     */
    protected fun showLoading() {
        setState { updateLoadingState(true) }
    }

    /**
     * 隐藏Loading状态
     */
    protected fun hideLoading() {
        setState { updateLoadingState(false) }
    }

    /**
     * 更新Loading状态
     */
    protected abstract fun S.updateLoadingState(isLoading: Boolean): S

    /**
     * 订阅Intent
     */
    private fun subscribeIntents() {
        viewModelScope.launch {
            _intent.collect {
                handleIntent(it)
            }
        }
    }

    /**
     * 发送Intent
     */
    fun setIntent(intent: I) {
        viewModelScope.launch { _intent.emit(intent) }
    }
}